package com.fare.taluo.core.domain.config.line.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.dayatang.domain.DataPage;
import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;
import com.fare.taluo.core.domain.config.line.OccupRateEntity;
import com.fare.taluo.core.domain.config.line.OccupRateEntityRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class OccupRateEntityRepositoryImpl extends EntityRepositoryHibernate
		implements OccupRateEntityRepository {
	
	@Inject
	private QueryChannelService queryChannel1;

	@Override
	public List<OccupRateEntity> findOccupRateEntityByCondition(
			String companyId, String lineCode, Date flightDay, int page,
			int pagesize) {
		List list = new ArrayList();
		StringBuffer sbf = new StringBuffer(
				"from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(companyId)) {
			sbf.append("and occup.companyCode =? ");
			list.add(companyId);
		}

		if (StringUtils.isNotEmpty(lineCode)) {
			sbf.append("and occup.lineCode like '"+lineCode+"%'");
			//list.add(lineCode);
		}
		
		if (flightDay != null) {
			DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			String day = date2String.format(flightDay); 
			String start = day+" 00:00:00";  
			    String end = day+" 23:59:59";
			sbf.append("and occup.flightDay between str_to_date(?,'yyyy-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
			list.add(start);
			list.add(end);
		}

		DataPage<OccupRateEntity> pageNum = find(sbf.toString(),
				list.toArray(), page, pagesize, OccupRateEntity.class);
		List<OccupRateEntity> lists = pageNum.getPageData();

		
		return lists;
	}
	
	public List<OccupRateEntity> findOccupRateEntityByCondition(String flightinfo, Date flightDay) {
		List list = new ArrayList();
		StringBuffer sbf = new StringBuffer(
				"from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(flightinfo)) {
			sbf.append("and occup.flightInfo =? ");
			list.add(flightinfo);
		}
	
		
		if (flightDay != null) {
			DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			String day = date2String.format(flightDay); 			
			String start = day+" 00:00:00";  
		    String end = day+" 23:59:59";
		    sbf.append("and occup.flightDay between str_to_date(?,'yyyy-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
		    list.add(start);
		    list.add(end);		 
		}

		List<OccupRateEntity> lists = super.find(sbf.toString(),
				list.toArray(), OccupRateEntity.class);	
		return lists;
	}

	// 得到符合条件的 总记录数
	public int getTotalOccupRateListByCodition(String companyId,String fromCity,String toCity, Date date) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(companyId)) {
			sbf.append("and occup.companyCode =? ");
			list.add(companyId);
		}

		if (StringUtils.isNotEmpty(fromCity)) {
			sbf.append("and occup.formCity =? ");
			list.add(fromCity);
		}
		if (StringUtils.isNotEmpty(toCity)) {
			sbf.append("and occup.toCity =? ");
			list.add(toCity);
		}

		if (date != null) {
			DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			String day = date2String.format(date); 
			String start = day+" 00:00:00";  
			 String end = day+" 23:59:59";
			sbf.append("and occup.flightDay between str_to_date(?,'yyyy-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
			list.add(start);
			list.add(end);
		}
	//	DataPage<OccupRateEntity> pageNum = super.find(sbf.toString(), list.toArray(), 1, 10, OccupRateEntity.class);
		Page<OccupRateEntity> pages = queryChannel1.queryPagedResultByPageNo(sbf.toString(), list.toArray(), 1, 10);
	/*	List<OccupRateEntity> lists = super.find(sbf.toString(),
				list.toArray(), OccupRateEntity.class);*/
		return (int)pages.getTotalCount();
		
	}
	
	// 得到符合条件的 总记录数
	public List<OccupRateEntity>  findOccupRateCondition2(String linkCode, String flightDay) {
		List list = new ArrayList();
		StringBuffer sbf = new StringBuffer(
				" from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(linkCode)) {
			sbf.append("and occup.lineCode =? ");
			list.add(linkCode);
		}
		if (flightDay != null) {
			///DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			String start = flightDay+" 00:00:00";  
			String end = flightDay+" 23:59:59";  
		    sbf.append("and occup.flightDay between str_to_date(?,'yyyy-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
		    list.add(start);
		    list.add(end);
		}
		List<OccupRateEntity> lists = super.find(sbf.toString(),
				list.toArray(), OccupRateEntity.class);	
		return lists;
		
	}
	public List<OccupRateEntity> findOccupRateEntityByCondition(String flightinfo, Date beginFlightDay,Date endFlightDay){
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(flightinfo)) {
			sbf.append("and occup.flightInfo =? ");
			list.add(flightinfo);
		}
		
		if (beginFlightDay != null&&endFlightDay!=null) {
			DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			String start = date2String.format(beginFlightDay)+" 00:00:00";  
			String end = date2String.format(endFlightDay)+" 23:59:59";  
		    sbf.append("and occup.flightDay between str_to_date(?,'yyyy-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
		    list.add(start);
		    list.add(end);
		}
		List<OccupRateEntity> lists = super.find(sbf.toString(),
				list.toArray(), OccupRateEntity.class);	
		return lists;
	}
	public List<OccupRateEntity>  findOccupRateEntity(String companyCode,String flightDay){
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(companyCode)) {
			sbf.append("and occup.companyCode =? ");
			list.add(companyCode);
		}
		
		if (flightDay != null) {
			///DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			/*String start = flightDay+" 00:00:00";  
			String end = flightDay+" 23:59:59";  
		    sbf.append("and occup.flightDay between str_to_date(?,'%Y-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
		    list.add(start);
		    list.add(end);*/
			sbf.append("and occup.flightDay = str_to_date(?,'%Y-%m-%d') ");
			list.add(flightDay);
		}
		List<OccupRateEntity> lists = super.find(sbf.toString(),
				list.toArray(), OccupRateEntity.class);	
		return lists;
	}

	@Override
	public List<OccupRateEntity> findOccupRateByCode(String companyId,
			String formCity, String toCity, Date date, int page, int pagesize) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from OccupRateEntity occup where 1=1 ");

		if (StringUtils.isNotEmpty(companyId)) {
			sbf.append("and occup.companyCode =? ");
			list.add(companyId);
		}

		if (StringUtils.isNotEmpty(formCity)) {
			sbf.append("and occup.formCity =? ");
			list.add(formCity);
		}
		if (StringUtils.isNotEmpty(toCity)) {
			sbf.append("and occup.toCity =? ");
			list.add(toCity);
		}
		if (date != null) {
			DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");    
			String day = date2String.format(date); 
			String start = day+" 00:00:00";  
			    String end = day+" 23:59:59";
			sbf.append("and occup.flightDay between str_to_date(?,'yyyy-mm-dd hh24:mi:ss') and str_to_date(?,'yyyy-mm-dd hh24:mi:ss') ");
			list.add(start);
			list.add(end);
		}
		
		Page<OccupRateEntity> pages = queryChannel1.queryPagedResultByPageNo(sbf.toString(), list.toArray(), page, pagesize);
	//	DataPage<OccupRateEntity> pageNum=	super.findByNamedQuery(sbf.toString(), list.toArray(), page, pagesize, OccupRateEntity.class);
		/*DataPage<OccupRateEntity> pageNum = super.find(sbf.toString(),
				list.toArray(), page, pagesize, OccupRateEntity.class);*/
		List<OccupRateEntity> lists = pages.getResult();
		return lists;
	}

}
